﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mengoo.DB;
using System.Collections;
using System.Data;

namespace Mengoo.BDMTier
{
    /// <summary>
    /// 数据操作层帮助类
    /// </summary>
    public class DAOHelper
    {
        /// <summary>
        /// 数据连接的类型
        /// </summary>
        public string ConnectionType
        {
            get;
            set;
        }

        /// <summary>
        /// 默认提供Sql的DBHelper
        /// </summary>
        public DAOHelper()
        {
        }

        #region Add
        /// <summary>
        /// 实例化
        /// </summary>
        /// <param name="connectionType"></param>
        public DAOHelper(string connectionType)
        {
            ConnectionType = connectionType;
        }


        /// <summary>
        /// 添加一条记录，指定tableName,指定哪些列不插入
        /// </summary>
        /// <param name="model">记录实体</param>
        /// <param name="tableName">表名</param>
        /// <param name="exceptColumns">不插入的列，用,分隔</param>
        /// <returns></returns>
        public virtual int Add(object model, string tableName, string exceptColumns)
        {
            IDBHelper helper = CreateDBHelper();
            string sql = SqlAdapt.AdaptInsertSql(model, tableName, exceptColumns);
            return helper.Execute(sql);
        }



        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="modelList">记录集合</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public virtual int Add(List<object> modelList, string tableName, string exceptColumns)
        {
            List<string> sqlList = SqlAdapt.AdaptInsertSql(modelList, tableName, "");
            IDBHelper helper = CreateDBHelper();
            return helper.ExecuteTran(sqlList);
        }


        /// <summary>
        /// 添加一条记录，指定表名
        /// </summary>
        /// <param name="columns">列的键值对</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public virtual int Add(Hashtable columns, string tableName)
        {
            string sql = SqlAdapt.AdaptInsertSql(columns, tableName);
            IDBHelper helper = CreateDBHelper();
            return helper.Execute(sql);
        }


        /// <summary>
        /// 添加多条记录，指定表名
        /// </summary>
        /// <param name="columnsList">列的键值对</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public virtual int Add(List<Hashtable> columnsList, string tableName)
        {
            List<string> sqlList = SqlAdapt.AdaptInsertSql(columnsList, tableName);
            IDBHelper helper = CreateDBHelper();
            return helper.ExecuteTran(sqlList);
        }
        #endregion

        #region Del
        /// <summary>
        /// 删除记录，根据条件
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="where">删除条件</param>
        /// <returns></returns>
        public virtual int Del(string tableName, string where)
        {
            IDBHelper helper = CreateDBHelper();
            string sql = SqlAdapt.AdaptDelSql(tableName, where);
            return helper.Execute(sql);
        }
        #endregion

        #region Update
        /// <summary>
        /// 更新记录，根据实体和关键字段名称
        /// </summary>
        /// <param name="model">数据实体</param>
        /// <param name="tableName">表名</param>
        /// <param name="keyField">更新条件</param>
        /// <returns></returns>
        public virtual int Update(object model, string tableName, string where)
        {
            IDBHelper helper = CreateDBHelper();
            string sql = SqlAdapt.AdaptUpdateSql(model, tableName, where);
            return helper.Execute(sql);
        }

        /// <summary>
        /// 更新记录，指定表名，根据列的键值对
        /// </summary>
        /// <param name="columns">列的键值对</param>
        /// <param name="tableName">表名</param>
        /// <param name="where">更新条件</param>
        /// <returns></returns>
        public virtual int Update(Hashtable columns, string tableName, string where)
        {
            string sql = SqlAdapt.AdaptUpdateSql(columns, tableName, where);
            IDBHelper helper = CreateDBHelper();
            return helper.Execute(sql);
        }

        /// <summary>
        /// 更新多条记录，提供需要更新的键值对集合，更新条件的集合，提交事务执行
        /// </summary>
        /// <param name="columns">列的键值对集合</param>
        /// <param name="tableName">表名</param>
        /// <param name="where">更新条件集合</param>
        /// <returns></returns>
        public virtual int Update(List<Hashtable> columnsList,string tableName, List<string> whereList)
        {
            List<string> sqlList = new List<string>();
            int i = 0;
            foreach (Hashtable columns in columnsList)
            {
                string where = i < whereList.Count ? whereList[i] : "";
                string sql = SqlAdapt.AdaptUpdateSql(columns, tableName, where);

                i++;
                sqlList.Add(sql);
            }
            IDBHelper helper = CreateDBHelper();
            return helper.ExecuteTran(sqlList);
        }
        #endregion

        #region GetTableList
        /// <summary>
        /// 获取列表，根据查询条件，指定表名，指定查询的列名
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="columns">列名</param>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        public DataTable GetTableList(string tableName, string columns, string where)
        {
            string sql = SqlAdapt.AdaptSelectSql(tableName, columns, where);
            IDBHelper helper = CreateDBHelper();
            return helper.GetDataTable(sql);
        }
        #endregion

        #region GetModel
        /// <summary>
        /// 获取Model
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">要查询的表名</param>
        /// <param name="where">查询条件</param>
        /// <param name="columns">要查询的列</param>
        /// <returns></returns>
        public T GetModel<T>(string tableName, string where, string columns)
        {
            DataTable dt = GetTableList(tableName, columns, where);
            return ModelHelper.DataTable2T<T>(dt);
        }

        /// <summary>
        /// 获取ModelList
        /// </summary>
        /// <typeparam name="T">要获取的对象类型</typeparam>
        /// <param name="where">要查询的表名</param>
        /// <param name="where">查询条件</param>
        /// <param name="columns">要查询的列</param>
        /// <returns></returns>
        public List<T> GetModelList<T>(string tableName, string where, string columns)
        {
            DataTable dt = GetTableList(tableName, columns, where);
            return ModelHelper.DataTable2List<T>(dt);
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 创建IDBHelper接口
        /// </summary>
        /// <returns></returns>
        private IDBHelper CreateDBHelper()
        {
            //读取webconfig中的配置作为数据库类型
            if (String.IsNullOrEmpty(ConnectionType))
            {
                return DBFactory.CreateDBHelper();
            }
            return DBFactory.CreateDBHelper(ConnectionType);
        }
        #endregion
    }
}
